import 'package:flutter/material.dart';
import 'package:gold/base/provider_widget.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/recover_cancel_reason_model.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// Descriptions: 黄金回收取消订单弹窗
/// User: tangzhentao
/// Date: 4:58 下午 2021/12/7
///
Future<int?> showRecoverCancelDialog() async {
  return await showModalBottomSheet<int>(
      context: XfgNav.currentContext,
      isScrollControlled: true,
      builder: (_) => _RecoverCancelDialog()
  );
}

class _RecoverCancelDialog extends StatelessWidget {
  late final _RecoverCancelViewmodel _cancelViewmodel;

  _RecoverCancelDialog() {
    _cancelViewmodel = _RecoverCancelViewmodel();
  }

  @override
  Widget build(BuildContext context) {
    final bottom = XfgSize().bottomBarHeight == 0 ? XfgSize().size10:XfgSize().bottomBarHeight;
    return Material(
      type: MaterialType.transparency,
      child: Container(
        height: XfgSize().size452 + bottom,
        padding: EdgeInsets.fromLTRB(XfgSize().size10, 0, XfgSize().size10, bottom),
        decoration: BoxDecoration(color: XfgColor.colorE, borderRadius: BorderRadius.vertical(top: Radius.circular(XfgSize().size6))),
        child: Column(
          children: [
            Container(
              height: XfgSize().size60,
              padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
              child: Row(
                children: [
                  SizedBox.square(dimension: XfgSize().size16),
                  Expanded(child: Center(child: XfgText('取消订单', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold))),
                  GestureDetector(
                    onTap: XfgNav.pop,
                    child: Icon(Icons.close, size: XfgSize().size16, color: XfgColor.text2),
                  )
                ],
              ),
            ),
            Expanded(
              child: Container(
                decoration: white6Decoration,
                child: ProviderWidget<_RecoverCancelViewmodel>(
                  model: _cancelViewmodel,
                  onInit: (data) => data.loadConfig(),
                  builder: (ctx, cancel) => ListView.builder(
                    itemCount: cancel.reasonList.length,
                    itemBuilder: (_, index) {
                      final reason = cancel.reasonList[index];
                      final select = index == cancel.selectIndex;
                      return GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () => cancel.changeSelect(index),
                        child: Container(
                          margin: EdgeInsets.only(top: index == 0 ? XfgSize().size18:XfgSize().size24),
                          padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                          child: Row(
                            children: [
                              Expanded(child: XfgText(reason.value?? '', fontSize: XfgSize().text16, color: XfgColor.text1D)),
                              Image.asset(select ? Res.imagesPayRadioS:Res.imagesPayRadio, width: XfgSize().size18, height: XfgSize().size18)
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                ),
              ),
            ),
            SizedBox(height: XfgSize().size16),
            TextButton(
              onPressed: _cancelViewmodel.confirmCancel,
              style: xfgButtonStyle(Size(XfgSize().size342, XfgSize().size50), radius: XfgSize().size50),
              child: XfgText('确认取消', fontSize: XfgSize().text16, color: XfgColor.white),
            )
          ],
        ),
      ),
    );
  }
}

class _RecoverCancelViewmodel extends Viewmodel {
  /// 取消原因列表
  final List<RecoverCancelReasonModel> reasonList = [];

  /// 当前选中原因下标
  int selectIndex = -1;

  /// 加载配置
  Future loadConfig() async {
    setLoading();
    final result = await Http().requestList<RecoverCancelReasonModel>(ApiGold.recycleGetRefundReason, showErrorMsg: false);
    if (result.success) {
      reasonList.clear();
      reasonList.addAll(result.data?? []);
      if (reasonList.isNotEmpty) {
        setSuccess();
      } else {
        setEmpty(msg: '暂无取消原因，请稍后重试');
      }
    } else {
      setFailed(msg: result.msg);
    }
  }

  /// 改变选中
  void changeSelect(int index) {
    if (selectIndex != index) {
      selectIndex = index;
      setSuccessChange();
    }
  }

  /// 确认取消
  void confirmCancel() {
    if (selectIndex >= 0) {
      XfgNav.pop(reasonList[selectIndex].id!);
    } else {
      '请选择取消原因'.info();
    }
  }

  @override
  onRetry() => loadConfig();

  @override
  onReload() => loadConfig();
}